package leetcode;

import java.util.Stack;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 东莞呵呵
 * Date:2022-10-15
 * Time:17:51
 */
public class TestDemo4 {
    public static boolean isValid(String s) {
        Stack<Character> stack = new Stack<>();
        for (int i = 0; i < s.length(); i++) {
            char ch = s.charAt(i);
            if (ch == '(' || ch == '[' || ch == '{') {
                stack.push(ch);
            } else if(ch != ')' && ch != '}' && ch != ']') {
                continue;
            }else {
                if (!stack.empty()) {
                    char top = stack.pop();
                    if (top == '(' && ch == ')' || top == '{' && ch == '}' || top == '[' && ch == ']') {
                        continue;
                    } else {
                        return false;
                    }
                } else {
                    return false;
                }
            }
        }
        if (!stack.empty()) {
            return false;
        }
        return true;
    }
    public static void main(String[] args) {
        String str="0";
        System.out.println(isValid(str));
    }
}
